/**
 * 
 */
package com.td.framework.webmanager.dlyh.service.impl;

import java.util.List;

import com.td.framework.webmanager.common.MD5;
import com.td.framework.webmanager.dlyh.dao.DlyhDao;
import com.td.framework.webmanager.dlyh.service.DlyhService;
import com.td.framework.webmanager.domain.AppsysDlyh;
import com.td.framework.webmanager.domain.AppsysDlyhBean;
import com.td.framework.webmanager.login.UserInfo;

/**
 * 
 * Title:用户信息业务层实现类 Description:实现业务层用户信息处理方法 Copyright: Copyright (c) 2008
 * Company 北京水软科技有限公司
 * 
 * @author FLT006
 * @version 1.0
 */
public class DlyhServiceImpl implements DlyhService {
	private DlyhDao dlyhDao = null;// 登录用户Dao;
	private String sql = "";	
    
	/**
	 * 检测用户时候是否存在
	 */
	public String checkUser(String yhm,String mima){
		return this.dlyhDao.checkUser(yhm,mima);
	}
	/**
	 * @toDo:保存用户信息;
	 * @param appsysDlyh
	 * @return boolean;
	 */
	public boolean addDlyh(AppsysDlyh appsysDlyh) {
		// TODO Auto-generated method stub
		return dlyhDao.addDlyh(appsysDlyh);
	}

	/**
	 * @toDo:删除用户信息;
	 * @param yhbhs
	 * @return boolean;
	 */
	public boolean deleteDlyh(String yhbhs) {
		// TODO Auto-generated method stub
		boolean flag = false;
		if (yhbhs != null) {
			flag = dlyhDao.deleteDlyh(yhbhs);
		}
		return flag;
	}

	/**
	 * @toDo: 查询所用的用户信息;
	 * @return list;
	 */
	@SuppressWarnings("unchecked")
	public List findALl() {
		// TODO Auto-generated method stub
		return dlyhDao.findAll();
	}

	/**
	 * @toDo:用户登录
	 * @param dlyhBean
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public UserInfo loadUserInfo(AppsysDlyhBean dlyhBean,String urlFlag) {
		UserInfo userInfo = null;
		if (dlyhBean != null) {
            String mima = dlyhBean.getMima();
            MD5 md5 = new MD5();// 实例化md5类;
            mima = md5.getMD5ofStr(mima);
			userInfo = this.dlyhDao.loadUserInfo(dlyhBean.getYhm(), mima);
		}
		return userInfo;
	}

	/**
	 * @toDo:通过yhbh查询用户信息
	 * @param yhbh
	 * @return AppsysDlyh;
	 */
	public AppsysDlyh getDlyh(String yhbh) {
		// TODO Auto-generated method stub
		return dlyhDao.getDlyh(yhbh);
	}

	/**
	 * @toDo:更新用户信息;
	 * @param appsysDlyh
	 * @param editFlag
	 * @return boolean;
	 */
	public boolean updateDlyh(AppsysDlyh appsysDlyh, int editFlag) {
		// TODO Auto-generated method stub
		return dlyhDao.updateDlyh(appsysDlyh, editFlag);
	}

	/**
	 * @toDo:密码还原
	 * @param yhbh
	 * @return flag
	 */
	public boolean initMima(String yhbh) {
		return dlyhDao.initMima(yhbh);
	}

	/**
	 * @toDo:get登录用户Dao;
	 * @return the dlyhDao
	 */
	public DlyhDao getDlyhDao() {
		return dlyhDao;
	}

	/**
	 * @toDo:set登录用户Dao;
	 * @param dlyhDao
	 *            the dlyhDao to set
	 */
	public void setDlyhDao(DlyhDao dlyhDao) {
		this.dlyhDao = dlyhDao;
	}

	/**
	 * @toDo:得到总记录数;
	 * @return rows;
	 */
	public int getRows() {
		return dlyhDao.getRows(sql);
	}

	/**
	 * @toDo:查询系统当前所有用户;
	 * @return list;
	 */
	public List<AppsysDlyhBean> findAppsysDlyhBean(String yhbh,String xtbm,String defaultBmbh) {
		setSql(null,yhbh,xtbm,defaultBmbh);
		return dlyhDao.getListPageData(sql, 100000, 0);
	}

	/**
	 * @toDo:查询用户组所属用户;
	 * @param yhzbh
	 * @param flag
	 *            0 当前用户组下用户,1 非当前用户组下用户
	 * @return list;
	 */
	public List<AppsysDlyhBean> findAppsysDlyhBeanByYhzbh(String yhzbh,
			int flag, String yhbh, String xtbm, String defaultBmbh) {
		String condtion = "";
		if (!yhbh.equals("-1")) {
			condtion = "and (c.bmbh in(select bm.bmbm from appsys_bmqx bm "
					+ "where ((bm.yhbh='" + yhbh + "' and bm.yhlb=1) or "
					+ "(bm.yhbh in (select t.yhbhz from appsys_yhyhzgx t "
					+ "where t.scbz=0 and t.yhbh='" + yhbh
					+ "') and bm.yhlb=0))" + " and bm.scbz=0 and bm.xtbm='"
					+ xtbm + "') or c.bmbh='" + defaultBmbh + "') ";
		}
		sql = "select distinct t.* from (select a.yhbh ,a.yhm,b.rybh ,"
				+ " b.xm,b.zw,d.bmbh,m.sjbmmc || d.bmmc as bmmc,"
				+ "a.mima,d.sjbmbh from appsys_dlyh a ,appsys_ryjbxx b ,"
				+ "appsys_rybmgx c,appsys_bmxx d    "
				+ " left join (select t.bmbh,ifnull(t.bmmc, '') || '>>' as sjbmmc "
				+ " from appsys_bmxx t where t.scbz = 0 and length(t.bmbh) >= 4 "
				+ ")m on m.bmbh=d.sjbmbh " + "where a.scbz=0 and a.yhbh in "
				+ "(select f.yhbh from appsys_yhyhzgx f where f.yhbhz ="
				+ yhzbh + ")" + " and b.rybh=a.rybh and c.rybh=b.rybh "
				+ condtion + " and d.bmbh = c.bmbh "
				+ "order by d.sjbmbh,d.pxh,bmmc,b.pxh ) t ";
		if (flag == 1) {
			sql = "select distinct t.* from (select a.yhbh ,a.yhm,b.rybh ,"
					+ " b.xm,b.zw,d.bmbh,m.sjbmmc || d.bmmc as bmmc,"
					+ "a.mima,d.sjbmbh from appsys_dlyh a ,appsys_ryjbxx b ,"
					+ "appsys_rybmgx c,appsys_bmxx d    "
					+ " left join (select t.bmbh,ifnull(t.bmmc, '') || '>>' as sjbmmc "
					+ " from appsys_bmxx t where t.scbz = 0 and length(t.bmbh) >= 4 "
					+ ")m on m.bmbh=d.sjbmbh "
					+ "where a.scbz=0 and a.yhbh not in "
					+ "(select f.yhbh from appsys_yhyhzgx f where f.yhbhz ="
					+ yhzbh + ")" + " and b.rybh=a.rybh and c.rybh=b.rybh  "
					+ condtion + " and d.bmbh = c.bmbh "
					+ "order by d.sjbmbh,d.pxh,bmmc,b.pxh ) t ";
		}
		return dlyhDao.getListPageData(sql, 100000, 0);
	}


	/**
	 * @toDo:查询系统用户
	 * @param yhzbh
	 * @param bmmc
	 * @param yhm
	 * @param xm
	 * @return list;
	 */
	public List<AppsysDlyhBean> findSysDlyh(String yhzbh, String bmmc,
			String yhm, String xm) {
		sql = "select distinct t.* from (select a.yhbh ,a.yhm,b.rybh ,"
				+ " b.xm,b.zw,d.bmbh,m.sjbmmc || d.bmmc as bmmc,"
				+ "a.mima,d.sjbmbh from appsys_dlyh a ,appsys_ryjbxx b ,"
				+ "appsys_rybmgx c,appsys_bmxx d    "
				+ " left join (select t.bmbh,ifnull(t.bmmc, '') || '>>' as sjbmmc "
				+ " from appsys_bmxx t where t.scbz = 0 and length(t.bmbh) >= 4 "
				+ ")m on m.bmbh=d.sjbmbh "
				+ "where a.scbz=0 and a.yhbh not in "
				+ "(select f.yhbh from appsys_yhyhzgx f where f.yhbhz ="
				+ yhzbh + ")"
				+ " and b.rybh=a.rybh and c.rybh=b.rybh and d.bmbh = c.bmbh "
				+ "order by d.sjbmbh,d.pxh,bmmc,b.pxh ) t where 1=1 ";
		if (!yhm.equals("") && !yhm.equals("null")) {
			sql += " and lower(t.yhm) like '%"
					+ ((yhm + "").trim()).toLowerCase() + "%' ";
		}
		if (!xm.equals("") && !xm.equals("null")) {
			sql += " and lower(t.xm) like '%"
					+ ((xm + "").trim()).toLowerCase() + "%' ";
		}
		if (!bmmc.equals("") && !bmmc.equals("null")) {
			sql += " and (t.bmmc like '%" + (bmmc + "").trim() + "%') ";
		}
		return dlyhDao.getListPageData(sql, 100000, 0);
	}

	/**
	 * 传入查询sql 语句返回列表数据
	 * 
	 * @return
	 * @throws Exception
	 */
	public List<AppsysDlyhBean> getListPageData(final String sql,
			final int pageSize, final int startRow) throws RuntimeException {
		return dlyhDao.getListPageData(sql, pageSize, startRow);
	}

	/**
	 * @toDo:set查询语句;
	 * @param AppsysDlyhBean
	 *            the sql to set
	 */
	public void setSql(AppsysDlyhBean dlyhBean,String yhbh,String xtbm,String defaultBmbh) {
		String condtion = "";
		if(!yhbh.equals("-1"))
		{
			condtion = "";
		}
		sql = "select a.yhbh ,a.yhm,b.rybh ,b.xm,b.zw,d.bmbh,t.sjbmmc||d.bmmc as bmmc,"
				+ "a.mima,a.id from appsys_dlyh a ,appsys_ryjbxx b ,"
				+ "appsys_rybmgx c,appsys_bmxx d "
				+ " left join (select t.bmbh,ifnull(t.bmmc,'')||'>>' as sjbmmc "
				+ " from appsys_bmxx t where t.scbz=0 and length(t.bmbh)>=4 ) t  on t.bmbh=d.sjbmbh "
				+ " where a.scbz=0 and "
				+ "b.rybh=a.rybh  "+condtion
				+ " and c.rybh=b.rybh and d.bmbh = c.bmbh ";
		
		if (dlyhBean != null) {
			String yhm = (dlyhBean.getYhm() + "").trim();
			String xm = (dlyhBean.getXm() + "").trim();
			String bmmc = (dlyhBean.getBmmc() + "").trim();
			if (!yhm.equals("") && !yhm.equals("null")) {
				sql += " and a.yhm like '%" + yhm.trim() + "%' ";
			}
			if (!xm.equals("") && !xm.equals("null")) {
				sql += " and b.xm like '%" + xm.trim() + "%' ";
			}
			if (!bmmc.equals("") && !bmmc.equals("null")) {
				sql += " and (t.sjbmmc like '%" + bmmc.trim()
						+ "%' or d.bmmc like '%" + bmmc.trim() + "%') ";
			}
		}
		sql += " order by d.pxh,d.bmmc,b.pxh ";
	}

	/**
	 * @toDo:get查询语句;
	 * @return the sql
	 */
	public String getSql() {
		return this.sql;
	}
}
